﻿using UnityEngine;
using System.Collections;
public class InputInfo : MonoBehaviour
{
	public float pixWidth  =1024;
	public float pixHeight = 768;
	
	public float walkingAreaHeight;
	public float walkingAreaBottom;
	public Vector2 walkingAreaCenter = Vector2.zero;
	
	Vector3 myTargetPoint;
	bool myFirstTouch = false;
	RaycastHit myHit;
	Camera myCamera;
	Vector3 coordinateOffset;
	float width;
	float height;
	bool mySingleTouch;
	bool myLongTouch;
	bool clicking;
	float startClickTime;
	float endClickTime;
    bool hitNpc = false;//Ö±œÓŸö¶šÊÇ·ñÓÐ»Øµ÷
	
	public Vector3 targetPoint {
		get {
			Vector3 temp = myTargetPoint - coordinateOffset;
			temp.x *= pixWidth / width;
			temp.y *= pixHeight / height;
			temp.x = (int)temp.x;
			temp.y = (int)temp.y;
			return temp;
		}
	}
	
	public bool firstTouch {
		get {
			return myFirstTouch;
		}
		set{
			myFirstTouch = value;
		}
	}
	
	public bool hasSingleTouch{
		get{
			return mySingleTouch;
		} 
		set{
			mySingleTouch = false;
		}
	}
	
	public bool hasLongTouch{
		get{
			return myLongTouch;
		}
	}

    public bool isHitNpc
    {
        get
        {
            return hitNpc;
        } 
		// 要删掉？只是为了符合playerAction的stop的回调
		set{
			hitNpc = value;
		}
    }
	
	public RaycastHit hit{
		get {
			return myHit;
		}
	}
	
	void Start(){
		myCamera = camera;
		
		//Screen.showCursor = false;
		//TODO
		
		coordinateOffset = new Vector3(width / 2,height / 2,0);
		walkingAreaHeight = Screen.height / 2.5f;
		walkingAreaBottom = Screen.height / 7;
		
	}

    string hitObjName = "";
	// Update is called once per frame
	void Update()
	{
		//#if UNITY_IPHONE
        /*
			if(Input.touchCount > 0 ){
				if(Input.GetTouch(0).phase == TouchPhase.Began){
					mySingleTouch = true;
					myLongTouch = false;
				
					Vector3 mousePosition = Input.GetTouch(0).position;
					
					if(mousePosition.y > walkingAreaHeight){
						mousePosition.y = walkingAreaHeight;
					}
					
					if(mousePosition.y < walkingAreaBottom){
						mousePosition.y = walkingAreaBottom;
					}
					
					myTargetPoint = mousePosition;
				    myFirstTouch = true;
					
					Ray ray = myCamera.ScreenPointToRay(myTargetPoint);
					Physics.Raycast(ray,out myHit,100);
				} else {
					mySingleTouch = false;
					myLongTouch = true;
				}
			} else {
				mySingleTouch = false;
				myLongTouch = false;
			}
         * */
		//#else

           

			if(Input.GetMouseButtonDown(0))
            {
                mouseClick();
			}
			
			if(!Input.GetMouseButton(0)){
				clicking = false;
				mySingleTouch = false;
				myLongTouch = false;
			}
		
			if(Time.time > startClickTime + 0.2f){
				if(clicking){
					mySingleTouch = false;
					myLongTouch = true;
				} else {
                    if (hitObjName == "Background")
                    {
                        mySingleTouch = true;
                    }
					myLongTouch = false;
				}
				startClickTime = 1000000;
			}
            //Debug.Log(mySingleTouch);
		
			/*
			if(Input.GetMouseButtonDown(0)){
				Vector3 mousePosition = Input.mousePosition;
				
				if(mousePosition.y > walkingAreaHeight){
					mousePosition.y = walkingAreaHeight;
				}
				
				if(mousePosition.y < walkingAreaBottom){
					mousePosition.y = walkingAreaBottom;
				}
				
				
				
				myTargetPoint = mousePosition;
			    
				
				Ray ray = myCamera.ScreenPointToRay(myTargetPoint);
				Physics.Raycast(ray,out myHit,100);
				
				if(hit.collider.tag.Equals("Background")){
					myFirstTouch = true;
					mySingleTouch = true;
				} else {
					myFirstTouch = false;
					mySingleTouch = false;
				}
			} else {
				mySingleTouch = false;
			}
			*/
		//#endif
	}

    public void mouseClick()
    {
        GameDefine.Click_Task_One = false;

        Vector3 mousePosition = Input.mousePosition;

        //if(mousePosition.y > walkingAreaHeight){
        //    mousePosition.y = walkingAreaHeight;
        //}

        //if(mousePosition.y < walkingAreaBottom){
        //    mousePosition.y = walkingAreaBottom;
        //}

        RaycastHit hit;
        RaycastHit hitGround;

        clicking = true;
        startClickTime = Time.time;

        //myTargetPoint = mousePosition;

        Ray ray = myCamera.ScreenPointToRay(mousePosition);

        if (Physics.Raycast(ray, out hit, 100))
        {
            hitObjName = hit.transform.name;
            //Debug.Log(hitObjName);
            //Debug.Log(hit.transform.name);
        }

        //RaycastHit[] hitObjs = Physics.RaycastAll(ray, 100);

        //foreach (RaycastHit val in hitObjs)
        //{
        //    Debug.Log(val.transform.name);
        //}
        //Debug.Log(hitObjs.Length);
        //if (hitObjs.Length == 0) return;
        //string hitObjName = hitObjs[0].transform.tag;
        //for(int i = 0;i < hitObjs.Length;i++)
        //{
        //    Debug.Log(hitObjs[i].transform.name);
        //}

        switch (hitObjName)
        {
            case "Background":
                {
                    if (mousePosition.y > walkingAreaHeight)
                    {
                        mousePosition.y = walkingAreaHeight;
                    }

                    if (mousePosition.y < walkingAreaBottom)
                    {
                        mousePosition.y = walkingAreaBottom;
                    }
                    Ray rayGround = myCamera.ScreenPointToRay(mousePosition);
                    Physics.Raycast(rayGround, out hitGround, 100);
                    hitNpc = false;
                    myHit = hitGround;
                    break;
                }
            case "NPC":
                {
                    hitNpc = true;
                    myHit = hit;
                    break;
                }
            case "Player":
                {
                    hitNpc = false;
                    myHit = hit;
                    break;
                }
            default:
                hitNpc = false;
                break;
        }

        //Debug.DrawRay(transform.position,ray.direction,Color.red);

        if (myHit.collider && myHit.collider.tag.Equals("Background"))
        {
            myFirstTouch = true;
        }
        else
        {
            myFirstTouch = false;
            mySingleTouch = false;
        }
    }
}
